package com.hq.fxh.No5_User.Jianli.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hq.fxh.CustomerView.PopupWindow.PopupForChangeInfo;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.JianLiWorkModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.TimeSelectUtils;
import com.hq.fxh.Utils.Tool;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static com.hq.fxh.R.id.tv_jianli_work_ZhiWei;
import static com.hq.fxh.R.id.tv_jianli_work_name;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/21
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_JianLi_AddWork extends Fragment implements View.OnClickListener {


    protected View rootView;
    protected TextView tvJianliWorkName;
    protected TextView tvJianliWorkZhiWei;
    protected TextView tvJianliWorkTimeIn;
    protected TextView tvJianliWorkNameOut;
    protected EditText editJianliWorkJingli;
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    protected TextView editNum;
    protected TextView jiagnliDeleteWork;
    PopupForChangeInfo popup;
    int type;
    int timetype;
    private TimeSelectUtils timeSelectUtils;
    private JianLiWorkModle jianLiWorkModle;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_jianliset_work, null);
        initView(rootView);
        initData();
        return rootView;
    }

    public void setData(String data) {
        if (data != null) {
            jianLiWorkModle = new Gson().fromJson(data, JianLiWorkModle.class);


        } else {
            jianLiWorkModle = new JianLiWorkModle();
        }
    }

    private void initData() {
        Tool.setStringToText(tvJianliWorkName, jianLiWorkModle.getCompany_name());
        Tool.setStringToText(tvJianliWorkZhiWei, jianLiWorkModle.getJob_name());
        Tool.setStringToText(tvJianliWorkTimeIn, jianLiWorkModle.getStart_dt());
        Tool.setStringToText(tvJianliWorkNameOut, jianLiWorkModle.getEnd_dt());
        toolBarTvTitle.setText("教育经历");
        toolBarTvMore.setText("保存");
        if (popup == null) {
            popup = new PopupForChangeInfo(getActivity(), new PopupForChangeInfo.ChangeCallBack() {
                @Override
                public void call(String comment) {
                    if (comment == null || comment.equals("")) {
                        LogUtils.startToash(getContext(), "填写的信息为空");
                    } else {
                        switch (type) {
                            case 0:
                                tvJianliWorkName.setText(comment);
                                jianLiWorkModle.setCompany_name(comment);
                                break;
                            case 1:
                                tvJianliWorkZhiWei.setText(comment);
                                jianLiWorkModle.setJob_name(comment);

                                break;


                        }
                    }


                }
            });
        }
    }

    private void initView(View rootView) {
        tvJianliWorkName = (TextView) rootView.findViewById(tv_jianli_work_name);
        tvJianliWorkZhiWei = (TextView) rootView.findViewById(tv_jianli_work_ZhiWei);
        tvJianliWorkTimeIn = (TextView) rootView.findViewById(R.id.tv_jianli_work_time_in);
        tvJianliWorkTimeIn.setOnClickListener(Fragment_JianLi_AddWork.this);
        tvJianliWorkNameOut = (TextView) rootView.findViewById(R.id.tv_jianli_work_name_out);
        tvJianliWorkNameOut.setOnClickListener(Fragment_JianLi_AddWork.this);
        tvJianliWorkName = (TextView) rootView.findViewById(R.id.tv_jianli_work_name);
        tvJianliWorkName.setOnClickListener(Fragment_JianLi_AddWork.this);
        tvJianliWorkZhiWei = (TextView) rootView.findViewById(R.id.tv_jianli_work_ZhiWei);
        tvJianliWorkZhiWei.setOnClickListener(Fragment_JianLi_AddWork.this);
        editJianliWorkJingli = (EditText) rootView.findViewById(R.id.edit_jianli_work_jingli);
        toolbarImageBack = (ImageView) rootView.findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(Fragment_JianLi_AddWork.this);
        toolBarTvTitle = (TextView) rootView.findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) rootView.findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_More);
        toolbarLayoutMore.setOnClickListener(Fragment_JianLi_AddWork.this);
        toolbarContent = (LinearLayout) rootView.findViewById(R.id.toolbar_Content);
        editNum = (TextView) rootView.findViewById(R.id.edit_Num);
        jiagnliDeleteWork = (TextView) rootView.findViewById(R.id.jiagnli_deleteWork);
        jiagnliDeleteWork.setOnClickListener(Fragment_JianLi_AddWork.this);

    }

    public void showEdit(View view, int type, String title) {
        this.type = type;
        popup.setTitle(title);
        popup.openKeyboard();
        Tool.setAlpha(getActivity(), 0.6f);
        popup.showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_jianli_work_name) {
            clickName();


        } else if (view.getId() == R.id.tv_jianli_work_ZhiWei) {
            clickZhiWei();


        } else if (view.getId() == R.id.tv_jianli_work_time_in) {
            clickTimeIn();


        } else if (view.getId() == R.id.tv_jianli_work_name_out) {
            clickTimeOut();

        } else if (view.getId() == R.id.toolbar_Layout_back) {
            getActivity().finish();

        } else if (view.getId() == R.id.toolbar_Layout_More) {
            clickBaoCun();

        } else if (view.getId() == R.id.jiagnli_deleteWork) {
            DeleteWork();

        }
    }

    private void clickBaoCun() {
        if (Tool.editIsEmpty(editJianliWorkJingli)) {
            LogUtils.startToash(getContext(), "工作内容不能为空");
        } else {
            String s = editJianliWorkJingli.getText().toString();
            jianLiWorkModle.setContent(s);
            jianLiWorkModle.setMember_id(SharedPreferenceUtils.getMemberId(getContext()));
            if (jianLiWorkModle.getWork_id() == 0) {
                addWork();
            }
        }


    }

    private void clickTimeOut() {
        timetype = 1;
        clickSelectTime();
    }

    private void clickTimeIn() {
        timetype = 0;
        clickSelectTime();
    }

    private void clickZhiWei() {
        showEdit(tvJianliWorkName, 1, "请输入从事职位");
    }

    private void clickName() {
        showEdit(tvJianliWorkName, 0, "请输入公公司名称");
    }

    public void clickSelectTime() {
        if (timeSelectUtils == null) {
            timeSelectUtils = new TimeSelectUtils();
        }

        timeSelectUtils.selectTime(getContext(), new TimeSelectUtils.TimeCallBack() {
            @Override
            public void seclectCall(DatePicker view, int year, int month, int dayOfMonth, String time) {
                if (timetype == 0) {
                    tvJianliWorkTimeIn.setText(time);
                    jianLiWorkModle.setStart_dt(time);

                } else if (timetype == 1) {
                    tvJianliWorkNameOut.setText(time);
                    jianLiWorkModle.setEnd_dt(time);

                }
            }
        });
    }


    public void addWork() {
        String s = new Gson().toJson(jianLiWorkModle);
        UserServiceImpl.upJianLiWork(s, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String string = null;
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("错误信息" + string1);
                        }
                    } else {
                        string = body.string();
                        LogUtils.e(string);
                        getActivity().finish();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("错误信息" + t.getMessage());

            }
        });

    }


    public void DeleteWork() {
        if(jianLiWorkModle.getWork_id()==0){
            getActivity().finish();
        }else {
            UserServiceImpl.deleteJianLiWork(jianLiWorkModle.getWork_id()+"", new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    String string = null;
                    try {
                        ResponseBody body = response.body();
                        if (body == null) {
                            ResponseBody responseBody = response.errorBody();
                            if (responseBody != null) {
                                String string1 = responseBody.string();
                                LogUtils.e("错误信息" + string1);
                            }
                        } else {
                            string = body.string();
                            LogUtils.e(string);
                            getActivity().finish();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    LogUtils.e("错误信息" + t.getMessage());

                }
            });
        }
    }

}
